package com.tianyu.jty.collector.service.extractor;

import com.google.common.collect.Lists;
import com.tianyu.jty.collector.entity.Site;
import com.tianyu.jty.collector.service.outputs.AbstractOutput;
import com.tianyu.jty.collector.utils.HttpClientUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.List;
import java.util.Set;

/**
 * Created by xtao on 2015/11/8.
 */
public class HtmlExtractor extends Extractor {
    private Set<String> keys;

    public HtmlExtractor() {
    }

    public HtmlExtractor(Site site, AbstractOutput outputer) {
        super(site);
        this.outputers.add(outputer);
        this.keys = site.getName2selector().keySet();
    }

    @Override
    public void extract() throws Exception {
        HttpClientUtil httpClientUtil = HttpClientUtil.getInctence();
        String string = httpClientUtil.doGet(site.getUrl());
        Document document = Jsoup.parse(string);
        for(String key: keys){
            Elements elements = document.select(site.getName2selector().get(key));
            List<Object> list = Lists.newArrayList();
            for (int i=0; i< elements.size();i++){

                list.add(extractContentByMethod(elements.get(i), key));
            }
            site.getResult().put(key, list);
        }
    }

    private Object extractContentByMethod(Element e, String key){
        Object result = "";
        if(site.getName2method()!=null){
            String method = site.getName2method().get(key);
            if("text".equals(method)){
                result = e.text();
            }else if("attr".equals(method)){

            }else {
                result = e.text();
            }
        }else {
            result = e.text();
        }
        return result;
    }

    private boolean isOnlyOne(){
        return site.getName2selector().size() == 1;
    }
}
